#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json

from app.common.dateutils import DateUtils
from app.core.constant_code import RedisConstant
from app.dao.instrument_dao import instrumentDao
from app.db.redis_session import redisClient
from app.models.bar import Bar
from app.schemas.bar import CandlestickQuery


class RedisManager:
    def __init__(self):
        pass

    def getLastCandlestick(self, candlestickQuery: CandlestickQuery):
        rsData = redisClient.get(RedisConstant.SPIDER_LAST_BARS.format(symbol=candlestickQuery.symbol, frequency=candlestickQuery.frequency))
        result = []
        if rsData:
            rsData = json.loads(rsData)
            dt = DateUtils.parse(rsData['barDate'])
            result.append(Bar(low=rsData['low'],
                              high=rsData['high'],
                              open=rsData['open'],
                              close=rsData['close'],
                              preClose=rsData['preClose'],
                              volume=rsData['volume'],
                              amount=rsData['amount'],
                              barDate=dt))
        return result


redisManager = RedisManager()
